package com.whfc.wxmp.api.emp.controller;

import com.whfc.common.base.BaseController;
import com.whfc.common.result.ListData;
import com.whfc.common.result.R;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.JSONUtil;
import com.whfc.wxmp.annotation.CurrentMsUser;
import com.whfc.wxmp.annotation.CurrentWxUser;
import com.whfc.emp.dto.*;
import com.whfc.emp.param.AppEmpAddParam;
import com.whfc.emp.param.AppEmpDeviceBindParam;
import com.whfc.emp.param.AppEmpEditParam;
import com.whfc.entity.dto.WxDeviceBindCheckDTO;
import com.whfc.entity.param.WxBindCheckParam;
import com.whfc.fuum.entity.SysUser;
import com.whfc.fuum.entity.WxUser;
import com.whfc.wxmp.api.emp.service.WxEmpService;
import com.whfc.wxmp.util.RequestConetxtUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 小程序中的人员列表
 *
 * @author guodong_sun
 * @Description:
 * @date 2019年11月4日
 */
@RestController
@RequestMapping("/mp/api/emp")
public class WxEmpController extends BaseController {

    @Autowired
    private WxEmpService wxEmpService;

    /**
     * 通过deptId查询所在项目所有班主长信息
     *
     * @param deptId 部门ID
     * @return 班组长信息列表
     */
    @GetMapping(value = "/leaderList")
    public R<List<AppEmpDTO>> leaderList(@RequestParam Integer deptId) {
        logger.info("查询部门下班组长列表, deptId: {}", deptId);
        // 调用现有服务获取所有人员列表
        List<AppEmpDTO> empDTOList = wxEmpService.getLeaderList(deptId, null);
        return R.success(empDTOList);
    }

    /**
     * 小程序中的人员列表(不分页)
     *
     * @param deptId
     * @param keyword
     * @return
     */
    @GetMapping(value = "/v1/list")
    public Result getEmpList(@CurrentWxUser WxUser loginUser,
                             @RequestParam("deptId") Integer deptId,
                             @RequestParam(value = "keyword", required = false) String keyword) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                loginUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "人员列表", RequestConetxtUtil.getParameters());
        ListData<WxEmpGroupDTO> listData = wxEmpService.getEmpList(deptId, keyword);
        return ResultUtil.success(listData);
    }

    @GetMapping(value = "/getEmpListByPhone")
    public Result getEmpList(
            @CurrentWxUser WxUser loginUser,
            @RequestParam("phone") String phone) {
        logger.info("基于手机号获取员工，手机号={},登录人={}",phone,loginUser.getId());
        return ResultUtil.success(wxEmpService.getEmpListByPhone(phone));
    }


    @GetMapping("/info")
    public Result empInfo(@CurrentWxUser WxUser wxUser,
                          @RequestParam(value = "empId") Integer empId) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                wxUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "查询人员信息", RequestConetxtUtil.getParameters());
        AppEmpDTO data = wxEmpService.getEmpInfo(wxUser, empId);
        return ResultUtil.success(data);
    }

    @PostMapping("/add")
    public Result bindEmp(@CurrentWxUser WxUser user, @Validated @RequestBody DeviceBindEmp request) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                user.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "硬件绑定人员", JSONUtil.toString(request));
        wxEmpService.bindEmp(user, request);
        return ResultUtil.success();
    }

    @PostMapping("/edit")
    public Result empEdit(@CurrentWxUser WxUser wxUser,
                          @RequestBody AppEmpEditParam request) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                wxUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "修改人员信息", JSONUtil.toString(request));
        wxEmpService.editEmpInfo(wxUser, request);
        return ResultUtil.success(null);
    }

    /**
     * 获取筛选人员工种列表
     *
     * @param user
     * @param deptId
     * @return
     */
    @GetMapping("/workTypeList")
    public Result workTypeList(@CurrentWxUser WxUser user, @RequestParam("deptId") Integer deptId) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                user.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "获取筛选人员工种列表", "{\"deptId\":\"" + deptId + "\"}");
        ListData<AppWorkTypeDTO> listData = wxEmpService.workTypeList(deptId);
        return ResultUtil.success(listData);
    }


    /**
     * 已存在人员绑定硬件
     *
     * @param user
     * @param request
     * @return
     */
    @PostMapping("/bind/device")
    public Result bindDevice(@CurrentWxUser WxUser user, @Validated @RequestBody AppEmpDeviceBindParam request) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                user.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "已存在人员绑定硬件", JSONUtil.toString(request));
        wxEmpService.bindDevice(user, request);
        return ResultUtil.success();
    }


    /**
     * 人员解绑硬件
     *
     * @param user
     * @param empId
     * @return
     */
    @PostMapping("/unbind/device/{empId}")
    public Result unbindDevice(@CurrentWxUser WxUser user, @PathVariable("empId") Integer empId) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                user.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "人员解绑硬件", "{\"empId\":\"" + empId + "\"}");
        wxEmpService.unbindDevice(user, empId);
        return ResultUtil.success();
    }


    /**
     * 绑定校验
     *
     * @param user
     * @param request
     * @return
     */
    @PostMapping("/bindCheck")
    public Result bindCheck(@CurrentWxUser WxUser user, @RequestBody @Validated WxBindCheckParam request) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                user.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "硬件绑定前检验", RequestConetxtUtil.getParameters());
        String sn = request.getSn();
        WxDeviceBindCheckDTO result = wxEmpService.bindCheck(sn);
        return ResultUtil.success(result);
    }

    @PostMapping("/del/{empId}")
    public Result delEmp(@CurrentWxUser WxUser user, @PathVariable("empId") Integer empId) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                user.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "删除人员", "{\"empId\":\"" + empId + "\"}");
        wxEmpService.delEmp(empId);
        return ResultUtil.success();
    }


    /**
     * 2.0 考勤中心人员列表(不分页)
     *
     * @param deptId
     * @param keyword
     * @return
     */
    @GetMapping(value = "/v2/list")
    public R<List<AppCorpGroupDTO>> getEmpListV2(@CurrentWxUser WxUser loginUser,
                             @RequestParam("deptId") Integer deptId,
                             @RequestParam(value = "keyword", required = false) String keyword) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                loginUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "人员列表", RequestConetxtUtil.getParameters());
        List<AppCorpGroupDTO> listData = wxEmpService.getEmpListV2(deptId, keyword);
        return R.success(listData);
    }

    /**
     * 2.0 人员信息登记-上传图片
     *
     * @param user
     * @param base64Str
     * @param file
     * @return
     * @throws IOException
     */
    @RequestMapping("/uploadImg")
    public R<String> uploadImg(@CurrentWxUser SysUser user,
                            @RequestParam(value = "base64Str", required = false) String base64Str,
                            @RequestParam(value = "file", required = false) MultipartFile file,
                            @RequestParam(value = "faceFlag", required = false) Boolean faceFlag) throws IOException {
        logger.info("员信息等级-上传图片,loginer:{}", user.getUsername());
        String path = wxEmpService.uploadImg(base64Str, file, faceFlag);
        return R.success(path);
    }


    /**
     * 2.0 人员信息登记-添加人员
     *
     * @param user
     * @param request
     * @return
     */
//    @RequiresPermissions("emp/list/add")
    @PostMapping("/add/emp")
    public R add(@CurrentMsUser SysUser user, @Validated @RequestBody AppEmpAddParam request) {
        logger.info("人员管理-添加人员，loginer:{},参数AppEmpAddParam:{}", user.getUsername(), request.toString());
        wxEmpService.add(request);
        return R.success();
    }


}
